package com.example.learnsqlite;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private DBManager dbManager;
    private List<Person> persons;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbManager = new DBManager(this);
        initView();
        initData();
        queryData();
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.listview);
    }

    private void initData() {
        persons = new ArrayList<Person>();

        Person person1 = new Person("AchillesL", 24, "a good man");
        Person person2 = new Person("dou bi xiaoxue", 24, "a good gril");
        Person person3 = new Person("Ella", 56, "lively girl");
        Person person4 = new Person("Jenny", 27, "beautiful girl");
        Person person5 = new Person("Jessica", 80, "sexy girl");
        Person person6 = new Person("Kelly", 56, "hot baby");
        Person person7 = new Person("Jane", 25, "a pretty woman");

        persons.add(person1);
        persons.add(person2);
        persons.add(person3);
        persons.add(person4);
        persons.add(person5);
        persons.add(person6);
        persons.add(person7);
    }

    private void insertData2DB() {
        dbManager.addList(persons);
    }

    private void updateAgeTest() {
        Person person = new Person();
        person.setName("Jenny");
        person.setAge(34);
        dbManager.updateAge(person);
    }

    private void deleteOldAgeTest() {
        dbManager.deleteOldPerson(23);
    }

    private void queryData() {
        Cursor cursor = dbManager.queryTheCursor();
        startManagingCursor(cursor);
        CursorWrapper cursorWrapper = new CursorWrapper(cursor);
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
                R.layout.item_listview, cursorWrapper, new String[] { "_id",
                        "name", "info" }, new int[] { R.id.tvNo, R.id.tvName,
                        R.id.tvInfo });
        Toast.makeText(this, cursorAdapter.getCount(), Toast.LENGTH_SHORT).show();
        listView.setAdapter(cursorAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        dbManager.closeDB();
    }
}
